package com.linkvin.ec.hcs.service.mdm.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.linkvin.ec.hcs.dao.mapper.mdm.VModelMapper;
import com.linkvin.ec.hcs.dao.model.mdm.VModel;
import com.linkvin.ec.hcs.dao.model.mdm.bo.VModelBo;
import com.linkvin.ec.hcs.service.mdm.VModelService;
import com.linkvin.ec.hcs.service.util.BaseException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class VModelServiceImpl implements VModelService {

    @Autowired
    private VModelMapper vModelMapper;

    @Override
    public List<VModel> listAllVehicleBySeriesId(Integer seriesId) {
        EntityWrapper<VModel> ew =new EntityWrapper<>();
        ew.eq("series_id",seriesId);
        return vModelMapper.selectList(ew);
    }

    @Override
    public List<VModelBo> queryPageVVehicle(Page<VModelBo> page) {
        List<VModelBo> listModelBo = vModelMapper.queryPageVModel(page,buildCondition(page.getCondition()));
        return listModelBo;
    }

    private EntityWrapper<VModelBo> buildCondition(Map<String,Object> condition){
        EntityWrapper<VModelBo> ew = new EntityWrapper<>();
        if(!Objects.equals(condition,null)) {
            for (Map.Entry<String, Object> entry : condition.entrySet()) {
                if (entry.getKey().equals("name")) {
                    ew.like("serices_name", entry.getValue().toString());
                }
                if(entry.getKey().equals("id")){
                    ew.eq("series_id", entry.getValue().toString());
                }
            }
        }
        return ew;
    }


    @Override
    public VModelBo addModel(VModelBo vModelBo) {
        //非空校验
        if (Strings.isNullOrEmpty(vModelBo.getModelName().trim())) throw new BaseException("车型名称不能为空");
        if (Objects.equals(vModelBo.getSeriesId(),null)) throw new BaseException("所属车系不能为空");

        //判断是否重复
        EntityWrapper<VModel> ew = new EntityWrapper<>();
        ew.where("model_name={0}",vModelBo.getModelName().trim());
        int count = vModelMapper.selectCount(ew);
        if(count > 0){
            throw new BaseException("车型名称重复");
        }
        VModel vModel = new VModel();
        BeanUtils.copyProperties(vModelBo, vModel);
        count = vModelMapper.insert(vModel);

        if(count > 0){
            vModelBo = getModelBO(vModel.getId());
        }
        return  vModelBo;
    }

    @Override
    public VModelBo updateModel(VModelBo vModelBo) {
        //非空校验
        if (Strings.isNullOrEmpty(vModelBo.getModelName().trim())) throw new BaseException("车型名称不能为空");
        if (Objects.equals(vModelBo.getSeriesId(),null)) throw new BaseException("所属车系不能为空");

        //判断是否重复
        EntityWrapper<VModel> ew = new EntityWrapper<>();
        ew.where("model_name={0}",vModelBo.getModelName().trim()).and("id !=",vModelBo.getId()).and("series_id =", vModelBo.getSeriesId());
        int count = vModelMapper.selectCount(ew);
        if(count > 0){
            throw new BaseException("车型名称重复");
        }

        VModel vModel = new VModel();
        BeanUtils.copyProperties(vModelBo, vModel);
        count = vModelMapper.updateById(vModel);
        if(count > 0){
            vModelBo = getModelBO(vModel.getId());
        }
        return  vModelBo;
    }

    @Override
    public int deleteModel(List<Integer> vModelBoIds) {
        //TODO 需要判断是否已经使用
        return vModelMapper.deleteBatchIds(vModelBoIds);
    }

    private VModelBo getModelBO(Integer id){
        VModelBo vModelBo = new VModelBo();
        List<VModelBo> listModelBo= vModelMapper.selectModelByID(id);
        if(!CollectionUtils.isEmpty(listModelBo)){
            vModelBo = listModelBo.get(0);
        }
        return vModelBo;
    }

}
